#include "CKDNS.h"
#include <netdb.h>
#include <sys/socket.h>
#include <arpa/inet.h>




const std::string GetHostByName(const std::string name)
{
	std::string ret = "";
	struct addrinfo hints, *result, *cur;
	int errno;
	
	hints = { 0 };
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	
	errno = getaddrinfo(name.c_str(), NULL, &hints, &result);
	if( 0 != errno)
	{
		return ret;
	}
	else
	{
		char ipstr[16];
		// for(cur = result ; cur != NULL ; cur = cur->ai_next)
		// {
		// 	inet_ntop(
		// 		AF_INET, 
		// 		&(((struct sockaddr_in *)(cur->ai_addr))->sin_addr),
		// 		ipstr,
		// 		16);
		// 	
		// }
		
		cur = result;
		inet_ntop(
			AF_INET, 
			&(((struct sockaddr_in *)(cur->ai_addr))->sin_addr),
			ipstr,
			16);
		ret += ipstr;
		freeaddrinfo(result);
		return ret;
	}
}